package br.com.futebol.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import br.com.futebol.R;
import br.com.futebol.vo.Jogador;
import br.com.futebol.vo.Time;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	private Context context;

    private ArrayList<Time> grupos;

    private ArrayList<ArrayList<Jogador>> filhos;

    public ExpandableListAdapter(Context context, ArrayList<Time> grupos,
            ArrayList<ArrayList<Jogador>> filhos) {
        this.context = context;
        this.grupos = grupos;
        this.filhos = filhos;
    }
	
	
    /**
     * A general add method, that allows you to add a Vehicle to this list
     * 
     * Depending on if the category opf the vehicle is present or not,
     * the corresponding item will either be added to an existing group if it 
     * exists, else the group will be created and then the item will be added
     * @param vehicle
     */
    public void addItem(List<Time> times) {
    	
    	for (Time time: times) {    	
    		if (!grupos.contains(time)) {
    			grupos.add(time);
    		}
    		filhos.add(time.getJogadores());
    	}
    	
    }

    public Object getChild(int groupPosition, int childPosition) {
        return filhos.get(groupPosition).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    	Jogador jogador = (Jogador) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        tv.setText("   " + jogador.getNome());

      
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return filhos.get(groupPosition).size();
    }

    public Object getGroup(int groupPosition) {
        return grupos.get(groupPosition);
    }

    public int getGroupCount() {
        return grupos.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
    	Time group = (Time) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group.getNome());
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
